﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;

namespace XDesigner.ORM
{
    internal class Util
    {
        public static bool IsImplementInterfaceType(Type instanceType, Type interfaceType)
        {
            Type[] its = instanceType.GetInterfaces();
            if (its != null)
            {
                foreach (Type it in its)
                {
                    if (it.Equals(interfaceType) || it.IsSubclassOf(interfaceType))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        public static string Contact(ORMDBValueType type, System.Collections.IEnumerable values)
        {
            StringBuilder result = new StringBuilder();
            foreach (object key in values )
            {
                if (key == null || DBNull.Value.Equals(key))
                {
                    continue;
                }
                string strKey = Convert.ToString(key);
                if (HasContent(strKey))
                {
                    if (result.Length > 0)
                    {
                        result.Append(",");
                    }
                    if (type == ORMDBValueType.Text || type == ORMDBValueType.DateTime)
                    {
                        result.Append("'" + strKey + "'");
                    }
                    else
                    {
                        result.Append(strKey);
                    }
                }
            }//foreach
            return result.ToString();
        }

        public static bool IsDBPrimitiveType(Type type)
        {
            if (type.IsPrimitive)
                return true;
            if (type.Equals(typeof(string))
                || type.Equals(typeof(DateTime))
                || type.Equals(typeof(decimal))
                || type.Equals(typeof(string))
                || type.Equals( typeof( byte[])))
                return true;
            return false;
        }

        public static string Contact(string[] items, string spliter)
        {
            StringBuilder str = new StringBuilder();
            foreach (string item in items)
            {
                if (str.Length > 0)
                    str.Append( spliter );
                str.Append(item);
            }
            return str.ToString();
        }

        public static Type GetType(string typeName)
        {
            return Type.GetType(typeName, false, true);
        }

        public static bool HasContent(string text)
        {
            return text != null && text.Trim().Length > 0;
        }


    }
}
